const PhoneDatasDao = appRequire(`dao/desktop/PhoneDatasSequelizeDao.js`);
const CarnumDatasDao = appRequire(`dao/desktop/CarnumDatasSequelizeDao.js`);
const CarnumDatas = appRequire('model/desktop/CarnumDatas');
const ResultVo = appRequire('vo/ResultVo.js');
// const SqlUtil = appRequire('modules/SqlUtil.js');
const uuid = require('uuid');
var dateFormat = require("dateformat");
const SqliteUtil = require('../../modules/SqliteUtil');
const FsUtil = appRequire('modules/FsUtil.js');
class CarnumDatasService {
    constructor() {

    }
    static async save(carnumDatas) {
        if (carnumDatas.id === '' || carnumDatas.id === undefined) {
            await CarnumDatasDao.saveOne(carnumDatas);
        } else {
            let dbObj = await CarnumDatasDao.findById(carnumDatas.id);
            // SqliteUtil.dbObjectToEntity(dbObj,carnumDatas);
            console.log(carnumDatas);
            await CarnumDatasDao.updateOne(carnumDatas);
        }
    }

    static async deleteOne(id) {
        return await CarnumDatasDao.deleteById(id);
    }

    static async deleteMany(ids) {
        ids = JSON.parse(ids);
        return await CarnumDatasDao.deleteMany(ids);
    }

    static async findOne(id) {
        return await CarnumDatasDao.findOne(id);
    }

    static async findAll() {
        return await CarnumDatasDao.findAll();
    }

    static async findByPage(page) {
        page.pageNum = parseInt(page.pageNum);
        page.pageSize = parseInt(page.pageSize);
        page.searchParams = JSON.parse(page.searchParams);
        return await CarnumDatasDao.findByPage(page);
    }

    static async publishData() {
        //在\dicms\data\private\tmp，构建一个dqk_db_日期.db。
        let dateTime = dateFormat(new Date(), "yyyy_mm_dd_HH_MM_ss");
        // dateTime = new Date();
        // dateTime = dateTime.getTime();
        console.log(dateTime);
        let dbPath = appConfig.rootUrl+`/public/dicms/data/private/tmp/dqk_db_${dateTime}.db`;
        console.log(dbPath);
        let fd = await FsUtil.open(dbPath);
        await FsUtil.close(fd);
        let sequelize = new Sequelize({
            dialect: 'sqlite',
            storage: dbPath,
            logging: console.log,
            define:{
              raw:true
            },
            
          });
        const AM = sequelize.define('androidMetadata',{
            locale:{
                field:"locale",
                type:DataTypes.TEXT,
                allowNull: false,
                defaultValue:'zh_CN',
                primaryKey:true,
            },
        },{
            modelName:"androidMetadata",
            tableName:'android_metadata',
            timestamps: false
        });
        const PD = sequelize.define('phoneDatas',{
            id:{
                field:"id",
                type:DataTypes.INTEGER(11),
                allowNull: false,
                primaryKey: true
            },
            firstLvDept:{
                field:"first_lv_dept",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            secondLvDept:{
                field:"second_lv_dept",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            username:{
                field:"username",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            job:{
                field:"job",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            phoneNum:{
                field:"phone_num",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            shortPhoneNum:{
                field:"short_phone_num",
                type:DataTypes.STRING(64),
                allowNull: true,
                defaultValue:'',
            },
            f7:{
                field:"f7",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            isHide:{
                field:"is_hide",
                type:DataTypes.STRING(10),
                allowNull: true,
                defaultValue:'0',
            },
            userSort:{
                field:"user_sort",
                type:DataTypes.INTEGER(11),
                allowNull: true,
                defaultValue:1,
            },
            firstLvDeptSort:{
                field:"first_lv_dept_sort",
                type:DataTypes.INTEGER(11),
                allowNull: true,
                defaultValue:1,
            },
            secondLvDeptSort:{
                field:"second_lv_dept_sort",
                type:DataTypes.INTEGER(11),
                allowNull: true,
                defaultValue:1,
            },
        },{
            modelName:"phoneDatas",
            tableName:'phone_datas',
            timestamps: false
        });
        const CD = sequelize.define('carnumDatas',{
            id:{
                field:"id",
                type:DataTypes.INTEGER(11),
                allowNull: false,
                primaryKey: true
            },
            carNum:{
                field:"car_num",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            carType:{
                field:"car_type",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            carColor:{
                field:"car_color",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            carDriver:{
                field:"car_driver",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            carDept:{
                field:"car_dept",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            carPhoneNum:{
                field:"car_phone_num",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            carAddress:{
                field:"car_address",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            carRegistTime:{
                field:"car_regist_time",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            carDriverNum:{
                field:"car_driver_num",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            carRemarks:{
                field:"car_remarks",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            isHide:{
                field:"is_hide",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            dataSort:{
                field:"data_sort",
                type:DataTypes.INTEGER,
                allowNull: true,
                defaultValue:1,
            },
        },{
            modelName:"carnumDatas",
            tableName:'carnum_datas',
            timestamps: false
        });
        const UPHD = sequelize.define('userPhoneHistoryDatas',{
            id:{
                field:"id",
                type:DataTypes.INTEGER(11),
                allowNull: false,
                primaryKey: true
            },
            firstLvDept:{
                field:"first_lv_dept",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            secondLvDept:{
                field:"second_lv_dept",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            username:{
                field:"username",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            job:{
                field:"job",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            phoneNum:{
                field:"phone_num",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            shortPhoneNum:{
                field:"short_phone_num",
                type:DataTypes.STRING(64),
                allowNull: true,
                defaultValue:'',
            },
            f7:{
                field:"f7",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            userSort:{
                field:"user_sort",
                type:DataTypes.INTEGER(11),
                allowNull: true,
                defaultValue:1,
            },
            firstLvDeptSort:{
                field:"first_lv_dept_sort",
                type:DataTypes.INTEGER(11),
                allowNull: true,
                defaultValue:1,
            },
            secondLvDeptSort:{
                field:"second_lv_dept_sort",
                type:DataTypes.INTEGER(11),
                allowNull: true,
                defaultValue:1,
            },
            phoneHistoryDatetime:{
                field:"phone_history_datetime",
                type:DataTypes.STRING(255),
                allowNull: true,
                defaultValue:'',
            },
            phoneHistoryState:{
                field:"phone_history_state",
                type:DataTypes.STRING(32),
                allowNull: true,
                defaultValue:'',
            },
        },{
            modelName:"userPhoneHistoryDatas",
            tableName:'user_phone_history_datas',
            timestamps: false
        });
        await sequelize.sync({ force: true });
        await AM.create({local:'zh_CN'});
        let phoneDatasList = await PhoneDatasDao.findAll();
        await PD.bulkCreate(phoneDatasList);
        let carnumDatasList = await CarnumDatasDao.findAll();
        await CD.bulkCreate(carnumDatasList);
        //将\phonemanage下的dqk_db.db备份dqk_db_日期.db，删除dqk_db.db,将第一步构建文件，移动过来
        let oldPath = appConfig.rootUrl+`/public/phonemanage/dqk_db.db`;
        let newPath = appConfig.rootUrl+`/public/phonemanage/dqk_db_${dateTime}.db`;
        await FsUtil.rename(oldPath,newPath);
        await FsUtil.copyFile(dbPath,oldPath);
        return "发布数据成功";
    }

}
module.exports = CarnumDatasService;